"""
学生信息管理模型
"""
from django.db import models
from django.conf import settings


class School(models.Model):
    """校区模型"""

    name = models.CharField(max_length=100, unique=True, verbose_name='校区名称')
    code = models.CharField(max_length=20, unique=True, verbose_name='校区缩写')
    principal = models.CharField(max_length=50, blank=True, null=True, verbose_name='负责人')
    phone = models.CharField(max_length=20, blank=True, null=True, verbose_name='联系电话')
    address = models.TextField(blank=True, null=True, verbose_name='校区地址')
    is_active = models.BooleanField(default=True, verbose_name='是否启用')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    
    class Meta:
        db_table = 'students_school'
        verbose_name = '校区'
        verbose_name_plural = '校区'
        ordering = ['name']
        
    def __str__(self):
        return self.name


class Major(models.Model):
    """专业模型"""

    name = models.CharField(max_length=100, unique=True, verbose_name='专业名称')
    description = models.TextField(blank=True, null=True, verbose_name='专业描述')
    is_active = models.BooleanField(default=True, verbose_name='是否启用')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'students_major'
        verbose_name = '专业'
        verbose_name_plural = '专业'
        ordering = ['name']

    def __str__(self):
        return self.name


class Grade(models.Model):
    """年级模型"""

    name = models.CharField(max_length=50, unique=True, verbose_name='年级名称')  # 如2024级
    description = models.CharField(max_length=100, blank=True, null=True, verbose_name='年级描述')  # 如"大二"
    is_active = models.BooleanField(default=True, verbose_name='是否启用')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'students_grade'
        verbose_name = '年级'
        verbose_name_plural = '年级'
        ordering = ['name']

    def __str__(self):
        return self.name



class Student(models.Model):
    """学生模型"""

    GENDER_CHOICES = [
        ('M', '男'),
        ('F', '女'),
    ]

    STATUS_CHOICES = [
        ('active', '在校'),
        ('graduated', '已毕业'),
        ('suspended', '休学'),
        ('dropped', '退学'),
    ]

    # 关联用户
    user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE,
                               null=True, blank=True, verbose_name='关联用户',
                               related_name='student_profile')

    # 基本信息
    student_id = models.CharField(max_length=20, unique=True, verbose_name='学号')
    name = models.CharField(max_length=50, verbose_name='姓名')
    gender = models.CharField(max_length=1, choices=GENDER_CHOICES, default='M', verbose_name='性别')
    birth_date = models.DateField(blank=True, null=True, verbose_name='出生日期')
    phone = models.CharField(max_length=20, blank=True, null=True, verbose_name='手机号')
    email = models.EmailField(blank=True, null=True, verbose_name='邮箱')

    # 学业信息
    school = models.ForeignKey(School, on_delete=models.CASCADE, blank=True, null=True, verbose_name='所属校区')
    major = models.ForeignKey(Major, on_delete=models.CASCADE, blank=True, null=True, verbose_name='所属专业')
    grade = models.ForeignKey(Grade, on_delete=models.CASCADE, verbose_name='所属年级')
    enrollment_date = models.CharField(max_length=7, blank=True, null=True, verbose_name='入学日期', help_text='格式：YYYY-MM')
    graduation_date = models.CharField(max_length=7, blank=True, null=True, verbose_name='毕业日期', help_text='格式：YYYY-MM')
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='active', verbose_name='学籍状态')
    advisor = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL,
                               null=True, blank=True, verbose_name='指导老师',
                               limit_choices_to={'role': 'teacher'})

    # 其他信息
    address = models.TextField(blank=True, null=True, verbose_name='家庭地址')
    notes = models.TextField(blank=True, null=True, verbose_name='备注')

    # 就业信息
    employment_status = models.CharField(max_length=20, choices=[
        ('unemployed', '未就业'),
        ('employed', '已就业'),
        ('self_employed', '自主创业'),
        ('further_study', '继续深造'),
    ], default='unemployed', verbose_name='就业状态')

    # 简历状态
    resume_status = models.CharField(max_length=20, choices=[
        ('not_started', '未开始'),
        ('in_progress', '制作中'),
        ('submitted', '已提交'),
        ('approved', '已通过'),
        ('rejected', '需修改'),
    ], default='not_started', verbose_name='简历状态')

    # 时间戳
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    
    class Meta:
        db_table = 'students_student'
        verbose_name = '学生'
        verbose_name_plural = '学生'
        ordering = ['student_id']
        
    def __str__(self):
        return f"{self.student_id} - {self.name}"


class TeacherStudentAssignment(models.Model):
    """老师学生分配关系"""

    teacher = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        limit_choices_to={'role': 'teacher'},
        verbose_name='老师'
    )
    student = models.ForeignKey(Student, on_delete=models.CASCADE, verbose_name='学生')
    assignment_type = models.CharField(max_length=20, choices=[
        ('advisor', '指导老师'),
        ('class_teacher', '班主任'),
        ('subject_teacher', '任课老师'),
    ], verbose_name='分配类型')
    assigned_date = models.DateTimeField(auto_now_add=True, verbose_name='分配时间')
    is_active = models.BooleanField(default=True, verbose_name='是否有效')

    class Meta:
        db_table = 'students_teacher_student_assignment'
        verbose_name = '师生分配关系'
        verbose_name_plural = '师生分配关系'
        unique_together = ['teacher', 'student', 'assignment_type']

    def __str__(self):
        return f"{self.teacher.username} - {self.student.name} ({self.get_assignment_type_display()})"
